package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.UserMoneyDao;
import com.gusha.model.ReceivingAddress;
import com.gusha.model.SysOrder;
import com.gusha.model.SysShopping;
import com.gusha.model.User;
import com.gusha.model.UserMoney;

@Service
@Transactional
public class UserMoneyService {
	  @Autowired
	  private UserMoneyDao userMoneyDao;
	  

		public Page<UserMoney> findAll(Pageable pageable, UserMoney userMoney) {
	        @SuppressWarnings("serial")
	        Page<UserMoney> result = userMoneyDao.findAll(new Specification<UserMoney>() {
	            @Override
	            public Predicate toPredicate(Root<UserMoney> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
	                List<Predicate> list = new ArrayList<Predicate>();
	                if (userMoney.getId() != null) {
	                    list.add(cb.equal(root.get("id").as(BigInteger.class),userMoney.getId()));
	                }
	      
	                
	                if (userMoney.getUser() != null) {
	                    list.add(cb.equal(root.get("userId").as(User.class),userMoney.getUser()));
	                }
	           
	                if (userMoney.getMoney() != null) {
	                    list.add(cb.equal(root.get("money").as(BigDecimal.class),userMoney.getMoney()));
	                }
	                
	                Predicate[] p = new Predicate[list.size()];
	                return cb.and(list.toArray(p));
	            }
	        },pageable);
	         return result;
	    }
}
